<?php
namespace App\Api\Xhzx;

use PhalApi\Api;


/**
 * 星火智芯APP接口
 * @author dogstar 20200331
 */
class Phone extends Api {
    public function getRules() {
        return array(
            'updateMoney' => array(
                'oaid' => array('name' => 'oaid', 'require' => true, 'min' => 1, 'max' => 50, 'desc' => 'oaid'),
                'types' => array('name' => 'types', 'require' => true, 'desc' => 'types'),
                'money' => array('name' => 'money', 'require' => true,  'desc' => 'money')
            )
        );
    }

    /**
     * 更新数据
     * @desc 更新帐号数据
     * @return int res 是否更新成功
     */
    public function updateMoney() {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *,x-token,X-token');
        
        $oaid = $this->oaid;
        $types = $this->types;
        $money = $this->money;

        $where = array( 
            'oaid'=>$oaid,
            'types'=>$types,
            'date'=>date('Y-m', time())
        );

        $mobile = \PhalApi\DI()->notorm->phone_list->where('oaid', $oaid)->fetchOne();
        if( !$mobile ){
            return 0;
        }

 
        $recrod = \PhalApi\DI()->notorm->phone_money->where($where)->fetchOne();
        if( $recrod ){
            return \PhalApi\DI()->notorm->phone_money->where('id', $recrod['id'])->update( array( 
                                                                                                    'username'=>$mobile['username'], 
                                                                                                    'mobile'=>$mobile['mobile'], 
                                                                                                    'name'=>$mobile['name'],
                                                                                                    'money'=>$money, 
                                                                                                    'update_time'=>date('Y-m-d H:i:s', time())
                                                                                                )
                                                                                            );
        }
        return \PhalApi\DI()->notorm->phone_money->insert(array(
                'username'=>$mobile['username'],
                'mobile'=>$mobile['mobile'],
                'name'=>$mobile['name'],
                'oaid'=>$oaid,
                'types'=>$types,
                'date'=>date('Y-m', time()),
                'money'=>$money,
                'create_time'=>date('Y-m-d H:i:s', time())
            )
        );

    }

}
